# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: bridge.py
  @time: 2021/5/18 10:29
  @desc:
"""

import sys
import os
from pathlib import Path

from PySide6.QtCore import QObject, Slot
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine

from style_rc import *


class Bridge(QObject):

    def __init__(self):
        super().__init__()
        # engine = QQmlApplicationEngine()
        self.engine = QQmlApplicationEngine()
        # Get the path of the current directory, and then add the name
        # of the QML file, to load it.
        qmlFile = Path(__file__).parent / 'view.qml'
        self.engine.load(os.fspath(qmlFile.resolve()))
        # self.engine.setProperty("con", self)
        # root signal
        # 获得窗口的根对象
        # self.my_obj = self.engine.rootObjects()[0]
        # self.my_obj.mySignal.connect(self.my_func)
        self.engine.rootContext().setContextProperty("con", self)
        # context.setContextProperty("con", self)

    # def my_func(self, my_string):
    #     # 根据objectName得到Text对象，并通过Property的text属性对Text赋值
    #     self.my_obj.findChild(QObject, 'tf').setProperty("text", my_string)

    @Slot(result=str)
    def getContext(self):
        return "75.6328"



    @Slot(str, result=str)
    def getColor(self, color_name):
        if color_name.lower() == "red":
            return "#ef9a9a"
        elif color_name.lower() == "green":
            return "#a5d6a7"
        elif color_name.lower() == "blue":
            return "#90caf9"
        else:
            return "white"

    @Slot(float, result=int)
    def getSize(self, s):
        print("getSize", s)
        size = int(s * 42)
        if size <= 0:
            return 1
        else:
            return size

    @Slot(str, result=bool)
    def getItalic(self, s):
        print("getItalic", s)
        if s.lower() == "italic":
            return True
        else:
            return False

    @Slot(str, result=bool)
    def getBold(self, s):
        if s.lower() == "bold":
            return True
        else:
            return False

    @Slot(str, result=bool)
    def getUnderline(self, s):
        if s.lower() == "underline":
            return True
        else:
            return False


if __name__ == '__main__':
    app = QGuiApplication(sys.argv)
    bridge = Bridge()
    sys.exit(app.exec())
